<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>段式回文</title>
</head>

<body>
  <script>
    var longestDecomposition = function (text) {
      let i = 0, j = text.length - 1;
      let a1 = "", a2 = "";
      let ans = 0;
      while (i < j) {
        a1 += text[i++];
        a2 = text[j--] + a2;
        if (a1 === a2) {
          ans += 2;
          a1 = a2 = ""
        }
      }
      return ans + (a1.length > 0 || i === j ? 1 : 0);
    };
    console.log(longestDecomposition("ghiabcdefhelloadamhelloabcdefghi"));
  </script>
</body>

</html>